<template>
	<view class="upload-comp"  @click="uploadImg">
		<slot></slot>
		<ToastComp v-if="toastInfo.visible" :toastInfo="toastInfo" @closeToast="closeToast" @canel="cancelFun"
			@cancel="confirmFun"></ToastComp>
	</view>
</template>

<script>
	import ToastComp from "@/components/ToastComp/ToastComp.vue"
	export default {
		props: {
			uploadInfo: {
				type: Object,
				default: () => {
					return {
						btnTxt: '上传文件',
						uploadTip: '支持拓展名：jpg,png,jpeg',
						fileList: [],
						project_id: '',
						topic_id: '',
						Id: ''

					}
				}
			},
			cmd:{
				type:String,
				default:''
			}
		},
		data() {
			return {
				STSTokenData: '',
				// toast
				toastInfo: {
					visible: false,
					type: 'success',
					title: '',
					subTitle: '',
					cancel: false,
					confirm: false,
					closeShow: true
				},
			}
		},
		watch: {
			uploadInfo: {
				handler() {
					// console.log(this.uploadInfo)
				},
				immediate: true,
				deep: true
			}
		},
		mounted() {
			// this.getSTSTokenFun()
		},
		methods: {
			// 上传头像
			uploadImg(){
				uni.chooseImage({
					count: 1, // 最多可以选择的图片张数，默认9
					sizeType: ['compressed'], //original 原图，compressed 压缩图，默认二者都有
					sourceType: ['album', 'camera'], //album 从相册选图，camera 使用相机，默认二者都有。如需直接开相机或直接选相册，请只使用一个选项
					success: (res) => {
						console.log(res)
						const tempFilePaths = res.tempFilePaths;
						let file = res.tempFiles[0]
						const type = file.name.substr(file.name.lastIndexOf(".")).toLowerCase();
						// 上传类型限制
						const fileType = [".png", ".jpeg", ".jpg"];
						if (!fileType.includes(type)) {
							let typeTxt = fileType.join(",");
							uni.showToast({
								title: `温馨提示：只支持${typeTxt}格式的文件`,
								icon: 'none',
								duration: 1500
							})
							return;
						}
						const isLt2M = file.size / 1024 / 1024 <= 2;
						if (!isLt2M) {
							uni.showToast({
								title: `文件最大不能超过2MB`,
								icon: 'none',
								duration: 1500
							})
							return;
						}
						this.updateAvatarFun(tempFilePaths)
					}
				})
			},
			// 上传头像函数
			updateAvatarFun(tempFilePaths){
				debugger
				uni.showLoading({
					title: '上传中',
					mask: true
				});
				let Authorization = 'Bearer ' + uni.getStorageSync('Authorization')
				const header = {
					Authorization: Authorization,
					'Accept-Version': '1.0.0'
				}
				debugger
				let _this = this;
				uni.uploadFile({
					url: _this.cmd,
					filePath: tempFilePaths[0],
					name: 'file',
					header,
					success: (uploadFileRes) => {
						debugger
						// 如果图片名称为image.jpg会有显示的问题，可能image会默认关键字
						console.log(uploadFileRes)
						uni.hideLoading()
						try{
							if(JSON.parse(uploadFileRes.data).code == 200){
								uni.showToast({
									title:'上传成功！',
									icon:'none'
								})
								this.$emit('cb', JSON.parse(uploadFileRes.data).data)
							} else {
								uni.showToast({
									title:'请上传标准的身份证头像面图片！'
								})
							}
						}catch(e){
							//TODO handle the exception
						}
						
					}
				});
			},
			// tosat
			closeToast() {
				this.toastInfo.visible = false
			},
			// 取消
			cancelFun() {
				this.toastInfo.visible = false
			},
			// 确认
			confirmFun() {
				this.toastInfo.visible = false
			}
		}
	}
</script>

<style lang="scss" scoped>

</style>
